/*
 * @Author: wupeiwen <javapeiwen2010@gmail.com>
 * @Date: 2022-03-31 11:03:58
 * @LastEditors: wupeiwen <javapeiwen2010@gmail.com>
 * @LastEditTime: 2023-03-21 18:25:29
 * @FilePath: /tea-garden-web/src/util/storeModules/system-login.js
 * @Description: 系统登录
 */
import { post } from '@/util/http.js'
import { ElMessage } from 'element-plus'
export default {
  state: {
    userInfo: '',
    loading: {
      userInfo: false
    }
  },
  mutations: {
    // 设置通用状态
    setCommonState (state, payload) {
      const { key, value } = payload
      state[key] = value
    },
    // 设置加载状态
    setLoadingState (state, payload) {
      const { key, value } = payload
      state.loading[key] = value
    }
  },
  actions: {
    async login ({ commit }, payload) {
      try {
        commit('setLoadingState', { key: 'userInfo', value: true })
        commit('setCommonState', { key: 'userInfo', value: '' })
        const { code, data, message } = await post({ url: '/userinfo/login', params: { userAccount: payload.username, password: payload.password } })
        if (code === 1) {
          commit('setCommonState', { key: 'userInfo', value: data })
          ElMessage({ message, type: 'success' })
          location.hash = '#/base-list'
        } else {
          ElMessage({ message, type: 'warning' })
        }
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'userInfo', value: false })
    }

  }
}
